home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-orca / orca / dbusserver.py < prev    next >
Encoding:
Python Source  |  2009-04-13  |  5.8 KB  |  163 lines

  1. # Orca
  2. #
  3. # Copyright 2008 Sun Microsystems Inc.
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Library General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 2 of the License, or (at your option) any later version.
  9. #
  10. # This library is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # Library General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Library General Public
  16. # License along with this library; if not, write to the
  17. # Free Software Foundation, Inc., Franklin Street, Fifth Floor,
  18. # Boston MA  02110-1301 USA.
  19.  
  20. """Exposes Orca as a DBus service for testing and watchdog purposes."""
  21.  
  22. __id__        = "$Id: dbusserver.py 4221 2008-09-15 08:11:23Z wwalker $"
  23. __version__   = "$Revision: 4221 $"
  24. __date__      = "$Date: 2008-09-15 04:11:23 -0400 (Mon, 15 Sep 2008) $"
  25. __copyright__ = "Copyright (c) 2008 Sun Microsystems Inc."
  26. __license__   = "LGPL"
  27.  
  28. import dbus
  29. import dbus.service
  30. import dbus.mainloop.glib
  31.  
  32. import debug
  33. import settings
  34.  
  35. # Handlers for logging speech and braille output.
  36. #
  37. loggingFileHandlers = {}
  38. loggingStreamHandlers = {}
  39.  
  40. # pylint: disable-msg=R0923
  41. # Server: Interface not implemented
  42.  
  43. class Server(dbus.service.Object):
  44.  
  45.     def __init__(self, object_path, bus_name):
  46.         dbus.service.Object.__init__(self, None, object_path, bus_name)
  47.  
  48.     @dbus.service.method(dbus_interface='org.gnome.Orca.Logging',
  49.                          in_signature='si', out_signature='')
  50.     def setDebug(self, debugFile, debugLevel):
  51.         """Sets the file to send detailed debug information."""
  52.         if not settings.enableRemoteLogging:
  53.             return
  54.         debug.println(debug.LEVEL_FINEST,
  55.                       "DBus Logging.setDebug(%s, %d)" \
  56.                       % (debugFile, debugLevel))
  57.         if debug.debugFile:
  58.             debug.debugFile.close()
  59.             debug.debugFile = None
  60.         if debugFile and len(debugFile):
  61.             debug.debugFile = open('%s.debug' % debugFile, 'w', 0)
  62.         debug.debugLevel = debugLevel
  63.  
  64.     @dbus.service.method(dbus_interface='org.gnome.Orca.Logging',
  65.                          in_signature='s', out_signature='')
  66.     def setLogFile(self, logFile):
  67.         """Sets the file to send speech and braille logging information."""
  68.         if not settings.enableRemoteLogging:
  69.             return
  70.         import logging
  71.         debug.println(debug.LEVEL_FINEST,
  72.                       "DBus Logging.setLogFile(%s)" % logFile)
  73.         for logger in ['braille', 'speech']:
  74.             log = logging.getLogger(logger)
  75.             formatter = logging.Formatter('%(message)s')
  76.             try:
  77.                 loggingFileHandlers[logger].flush()
  78.                 loggingFileHandlers[logger].close()
  79.                 log.removeHandler(loggingFileHandlers[logger])
  80.             except:
  81.                 pass
  82.             if logFile and len(logFile):
  83.                 loggingFileHandlers[logger] = logging.FileHandler(
  84.                     '%s.%s' % (logFile, logger), 'w')
  85.                 loggingFileHandlers[logger].setFormatter(formatter)
  86.                 log.addHandler(loggingFileHandlers[logger])
  87.             log.setLevel(logging.INFO)
  88.  
  89.     @dbus.service.method(dbus_interface='org.gnome.Orca.Logging',
  90.                          in_signature='', out_signature='')
  91.     def startRecording(self):
  92.         """Tells Orca to start logging speech and braille output."""
  93.         if not settings.enableRemoteLogging:
  94.             return
  95.         debug.println(debug.LEVEL_FINEST, "DBus Logging.startRecording")
  96.         import logging
  97.         import StringIO
  98.         for logger in ['braille', 'speech']:
  99.             log = logging.getLogger(logger)
  100.             try:
  101.                 [stringIO, handler] = loggingStreamHandlers[logger]
  102.                 handler.close()
  103.                 log.removeHandler(handler)
  104.                 stringIO.close()
  105.             except:
  106.                 pass
  107.             formatter = logging.Formatter('%(message)s')
  108.             stringIO = StringIO.StringIO()
  109.             handler = logging.StreamHandler(stringIO)
  110.             handler.setFormatter(formatter)
  111.             log.addHandler(handler)
  112.             loggingStreamHandlers[logger] = [stringIO, handler]
  113.             log.setLevel(logging.INFO)
  114.  
  115.     @dbus.service.method(dbus_interface='org.gnome.Orca.Logging',
  116.                          in_signature='', out_signature='s')
  117.     def stopRecording(self):
  118.         """Tells Orca to stop logging speech and braille output and
  119.         to return whatever was recorded since the last call to
  120.         startRecording."""
  121.         if not settings.enableRemoteLogging:
  122.             return ""
  123.         debug.println(debug.LEVEL_FINEST, "DBus Logging.stopRecording")
  124.         import logging
  125.         import StringIO
  126.         result = ''
  127.         for logger in ['braille', 'speech']:
  128.             log = logging.getLogger(logger)
  129.             try:
  130.                 [stringIO, handler] = loggingStreamHandlers[logger]
  131.                 handler.flush()
  132.                 handler.close()
  133.                 log.removeHandler(handler)
  134.                 result += stringIO.getvalue()
  135.                 stringIO.close()
  136.             except:
  137.                 debug.printException(debug.LEVEL_OFF)
  138.             stringIO = StringIO.StringIO()
  139.         return result
  140.  
  141. obj = None
  142.  
  143. def init():
  144.     """Sets up the Orca DBus service.  This will only take effect once
  145.     the Orca main loop starts."""
  146.  
  147.     global obj
  148.  
  149.     if obj:
  150.         return
  151.  
  152.     try:
  153.         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
  154.         bus = dbus.SessionBus()
  155.         name = dbus.service.BusName('org.gnome.Orca', bus=bus)
  156.         obj = Server('/', name)
  157.     except:
  158.         debug.println(debug.LEVEL_WARNING,
  159.                       "dbusserver.py: Could not initialize DBus server")
  160.  
  161. def shutdown():
  162.     pass
  163.